﻿using DataAccess;
using KhoaLuan.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KhoaLuan.MainSys.ProcessAJAX
{
    public partial class AjaxPower : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static string GetListGroup(string keyword, int numberinpage, int currentpage)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementPower.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Edit))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xem danh sách phân quyền" });
            try
            {
                var pr = new tbGroup_Proxy();
                var powerproxy = new tbPowers_Proxy();
                var menuproxy = new tbMenu_Proxy();
                var listresult = pr.SelectAll_tbGroup_Proxy().ToList<tbGroup>();
                var temp = listresult.Skip(numberinpage * (currentpage - 1)).Take(numberinpage);
                var listobject = new List<object>();
                var listmenu = menuproxy.SelectAll_tbMenu_Proxy().ToList<tbMenu>();
                foreach (var item in temp)
                {
                    var listpower = powerproxy.GetByGroupID(item.GroupId).GroupBy(x => x.MenuID).Select(x => x.First());
                    var listcontrol = new List<object>();
                    foreach (var item2 in listpower)
                    {
                        var menu = listmenu.Single(x => x.MenuID == item2.MenuID);
                        bool addnew = false, view = false, edit = false, del = false;
                        if (SecurityServices.HasPermison(Utils.TypeAudit.View, item2.NumberPermison))
                            view = true;
                        if (SecurityServices.HasPermison(Utils.TypeAudit.AddNew, item2.NumberPermison))
                            addnew = true;
                        if (SecurityServices.HasPermison(Utils.TypeAudit.Edit, item2.NumberPermison))
                            edit = true;
                        if (SecurityServices.HasPermison(Utils.TypeAudit.Delete, item2.NumberPermison))
                            del = true;
                        var data = new { MenuID = menu.MenuID, MenuName = menu.MenuName, Path = menu.Link, View = view, AddNew = addnew, Edit = edit, Delete = del };
                        listcontrol.Add(data);
                    }
                    var group = new
                    {
                        GroupID = item.GroupId,
                        GroupName = item.GroupName,
                        ListControl = listcontrol
                    };
                    listobject.Add(group);
                }

                return new JavaScriptSerializer().Serialize(new { Status = true, Data = listobject, TotalItem = listresult.Count() });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }

        [WebMethod]
        public static string SaveChangePermission(List<int> powerid, List<int> menuid, List<bool> view, List<bool> add, List<bool> edit, List<bool> del, int groupid)
        {
            try
            {
                var proxy = new tbPowers_Proxy();
                var proxymenu = new tbMenu_Proxy();
                var proxygroup = new tbGroup_Proxy();
                for (int i = 0; i < powerid.Count; i++)
                {
                    if (powerid[i] != 0)
                    {
                        var power = proxy.GetById_tbPowers_Proxy(powerid[i]).ToObject<tbPowers>();
                        var numberpermision = (view[i] ? (int)TypeAudit.View : 0) | (add[i] ? (int)TypeAudit.AddNew : 0) | (edit[i] ? (int)TypeAudit.Edit : 0) | (del[i] ? (int)TypeAudit.Delete : 0);
                        if (power != null)
                        {
                            if (numberpermision != 0)
                            {
                                power.MenuID = menuid[i];
                                power.GroupID = groupid;
                                power.NumberPermison = (view[i] ? (int)TypeAudit.View : 0) | (add[i] ? (int)TypeAudit.AddNew : 0) | (edit[i] ? (int)TypeAudit.Edit : 0) | (del[i] ? (int)TypeAudit.Delete : 0);
                                if (!proxy.Update_tbPowers_Proxy(power))
                                    return new JavaScriptSerializer().Serialize(new { Status = false });
                            }
                            else
                            {
                                if (!proxy.Delete_tbPowers_Proxy(power.PowersID))
                                    return new JavaScriptSerializer().Serialize(new { Status = false });
                            }
                        }
                    }
                    else
                    {
                        var numberpermision = (view[i] ? (int)TypeAudit.View : 0) | (add[i] ? (int)TypeAudit.AddNew : 0) | (edit[i] ? (int)TypeAudit.Edit : 0) | (del[i] ? (int)TypeAudit.Delete : 0);
                        if (numberpermision != 0)
                        {
                            var power = new tbPowers()
                                {
                                    GroupID = groupid,
                                    MenuID = menuid[i],
                                    NumberPermison = numberpermision
                                };
                            if (!proxy.Insert_tbPowers_Proxy(power))
                                return new JavaScriptSerializer().Serialize(new { Status = false });
                        }
                    }
                }
                return new JavaScriptSerializer().Serialize(new { Status = true });

            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }
    }
}